package com.river.springbootdemo.thinkingjava.objectrefer;

import java.util.*;

/**
 * adding several Adapter Methods
 */
public class MultiIterableClass implements Iterable<String> {
    protected String[] words = ("And that is how we know the Earth to be banana-shaped.").split(" ");

    public Iterator<String> iterator() {
        return new Iterator<String>() {
            private int index = 0;
            public boolean hasNext() { return index < words.length; }
            public String next() { return words[index++]; }
            public void remove() { throw new UnsupportedOperationException();  }
        };

    }

    public Iterable<String> reversed() {
        return () -> new Iterator<String>() {
            int current = words.length - 1;
            @Override
            public boolean hasNext() { return current > -1; }
            @Override
            public String next() { return words[current--]; }
            public void remove() { throw new UnsupportedOperationException(); }
        };
    }

    public Iterable<String> randomized() {
        return () -> {
            List<String> shuffled = new ArrayList<>(Arrays.asList(words));
            Collections.shuffle(shuffled, new Random(47));
            return shuffled.iterator();
        };
    }

    public static void main(String[] args) {
        MultiIterableClass mic = new MultiIterableClass();
        for (String s : mic.reversed()) {
            System.out.print(s+" ");
        }
        System.out.println();
        for (String s : mic.randomized()) {
            System.out.print(s+" ");
        }
        System.out.println();
        for (String s : mic) {
            System.out.print(s+" ");
        }
    }
}
